<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - baked illumination</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#000;
				color:#000;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:monospace;
				font-size:13px;
				text-align:center;
			}

			a { color:skyblue }

			#info {
				position: absolute;
				top: 0px; width: 100%;
				color: #fff;
				background:rgba(0,0,0,0.65);
				padding: 5px;
				text-align: center;
				z-index:100;
				display:block;
			}
		</style>
	</head>

	<body>
		<div id="info">
		<a href="http://threejs.org" target="_blank">three.js</a> webgl -
		using <a href="http://code.google.com/p/js-openctm/">js-openctm</a> -
		camaro by <a href="http://www.turbosquid.com/3d-models/blender-camaro/411348" target="_blank">dskfnwn</a> -
		skybox by <a href="http://ict.debevec.org/~debevec/" target="_blank">Paul Debevec</a>
		</div>

		<script src="../build/three.js"></script>

		<script src="js/controls/OrbitControls.js"></script>

		<script src="js/loaders/ctm/lzma.js"></script>
		<script src="js/loaders/ctm/ctm.js"></script>
		<script src="js/loaders/ctm/CTMLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;
			var FLOOR = -250;

			var container, stats;

			var camera, scene, controls;
			var renderer;

			var mesh;

			var textureCube;

			var loader;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				// CAMERA

				camera = new THREE.PerspectiveCamera( 30, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
				camera.position.set( 185, 40, 170 );

				controls = new THREE.OrbitControls( camera );
				controls.maxPolarAngle = Math.PI / 2;
				controls.minDistance = 150;
				controls.maxDistance = 500;

				// SCENE

				var r = "textures/cube/pisa/";
				var urls = [
					r + "px.png", r + "nx.png",
					r + "py.png", r + "ny.png",
					r + "pz.png", r + "nz.png"
				];

				textureCube = new THREE.CubeTextureLoader().load( urls );

				scene = new THREE.Scene();
				scene.background = textureCube;

				// LIGHTS

				var light = new THREE.PointLight( 0xffffff, 1 );
				light.position.set( 2, 5, 1 );
				light.position.multiplyScalar( 30 );
				scene.add( light );

				var light = new THREE.PointLight( 0xffffff, 0.75 );
				light.position.set( -12, 4.6, 2.4 );
				light.position.multiplyScalar( 30 );
				scene.add( light );

				scene.add( new THREE.AmbientLight( 0x050505 ) );

				// RENDERER

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
				renderer.domElement.style.position = "relative";

				container.appendChild( renderer.domElement );

				//

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				// STATS

				stats = new Stats();
				container.appendChild( stats.dom );

				// EVENTS

				window.addEventListener( 'resize', onWindowResize, false );
				window.addEventListener( 'mousemove', onDocumentMouseMove, false );

				// LOADER

				var start = Date.now();

				// new way via CTMLoader and separate parts

				loaderCTM = new THREE.CTMLoader();

				var position = new THREE.Vector3( -105, -78, -30 );
				var scale = new THREE.Vector3( 30, 30, 30 );

				loaderCTM.loadParts( "models/ctm/camaro/camaro.js", function( geometries, materials ) {

					hackMaterials( materials );

					for ( var i = 0; i < geometries.length; i ++ ) {

						var mesh = new THREE.Mesh( geometries[ i ], materials[ i ] );
						mesh.position.copy( position );
						mesh.scale.copy( scale );
						scene.add( mesh );

					}

					var end = Date.now();

					console.log( "load time:", end - start, "ms" );

				}, { useWorker: true } );

			}

			//

			function hackMaterials( materials ) {

				for ( var i = 0; i < materials.length; i ++ ) {

					var m = materials[ i ];

					if ( m.name.indexOf( "Body" ) !== -1 ) {

						var mm = new THREE.MeshStandardMaterial();

						mm.color.setHex( 0x000000 );
						mm.lightMap = m.map;
						mm.envMap = textureCube;
						mm.metalness = 0.5;
						mm.roughness = 0.3;

						materials[ i ] = mm;

					} else if ( m.name.indexOf( "tire_car" ) !== -1 ) {

						var mm = new THREE.MeshStandardMaterial();

						mm.color.setHex( 0x000000 );
						mm.lightMap = m.map;
						mm.metalness = 0.1;
						mm.roughness = 0.9;

						materials[ i ] = mm;

					} else if ( m.name.indexOf( "mirror" ) !== -1 ) {

						var mm = new THREE.MeshStandardMaterial();

						mm.color.setHex( 0x808080 );
						mm.lightMap = m.map;
						mm.envMap = textureCube;
						mm.metalness = 0.9;
						mm.roughness = 0.5;

						materials[ i ] = mm;

					} else if ( m.name.indexOf( "glass" ) !== -1 ) {

						var mm = new THREE.MeshStandardMaterial();

						mm.color.copy( m.color );
//						mm.lightMap = m.map;
						mm.envMap = textureCube;
						mm.metalness = 1;
						mm.roughtness = 0.1;
						mm.opacity = m.opacity;
						mm.transparent = true;

						materials[ i ] = mm;

					} else if ( m.name.indexOf( "Material.001" ) !== -1 ) {

						var mm = new THREE.MeshPhongMaterial( { map: m.map } );

						mm.specularMap = m.map;
						mm.shininess = 30;
						mm.color.setHex( 0x404040 );
						mm.metal = true;

						materials[ i ] = mm;

					}

					materials[ i ].side = THREE.DoubleSide;

				}

			}

			//

			function createScene( geometry, materials, x, y, z, s ) {

				geometry.center();

				hackMaterials( materials );

				var material = new THREE.MultiMaterial( materials );

				mesh = new THREE.Mesh( geometry, material );
				mesh.position.set( x, y, z );
				mesh.scale.set( s, s, s );
				scene.add( mesh );

			}

			//

			function onWindowResize( event ) {

				SCREEN_WIDTH = window.innerWidth;
				SCREEN_HEIGHT = window.innerHeight;

				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );

				camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
				camera.updateProjectionMatrix();

			}


			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				controls.update();

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
